﻿using System;
using Avalonia.Controls;
using DevManager.ViewModels.Pages;
using FluentAvalonia.UI.Controls;

namespace DevManager.Views;

class NavigationFactory : INavigationPageFactory
{
    // Create a page based on a Type, but you can create it however you want
    public Control GetPage(Type srcType)
    {
        // Return null here because we won't use this method at all
        return null!;
    }

    // Create a page based on an object, such as a view model
    public Control GetPageFromObject(object target)
    {
        if (target is PageModelBase)
        {
            var name = target.GetType().FullName!.Split(".")[^1].Replace("Model", "");
            name = $"DevManager.Pages.{name}";
            var type = Type.GetType(name);
            if (type != null)
            {
                var c = (Control)Activator.CreateInstance(type)!;
                c.DataContext = target;
                return c;
            }

            return new TextBlock { Text = "Not Found: " + name };
        }

        if (target is Control control)
        {
            return control;
        }

        throw new Exception();
    }
}